package edu.spec.sms.controller;

import edu.spec.sms.model.lesson.Lesson;
import edu.spec.sms.service.iface.ICourseService;
import edu.spec.sms.service.iface.ILessonService;
import edu.spec.sys.model.dto.MvcReturn;
import edu.spec.sys.model.enumeration.Status;
import edu.spec.sys.util.BarcodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by MadFrog on 2017/10/13.
 */
@Controller
@RequestMapping("/admin/lesson")
public class LessonController {

    @Autowired
    private ILessonService lessonService;
    @Autowired
    private ICourseService courseService;

    @ResponseBody
    @RequestMapping("/listByCourse")
    public MvcReturn listByCourse(Integer courseId){
        List<Lesson> list = lessonService.listLessonByCourse(courseId);
        return new MvcReturn(true, list);
    }

    @ResponseBody
    @RequestMapping("/add")
    public MvcReturn addLesson(Lesson l, Integer courseId){
        if (courseId==null){
            return new MvcReturn(false, "请选择课程");
        }
        l.setCourse(courseService.load(courseId));
        lessonService.add(l);
        return new MvcReturn(true);
    }

    @ResponseBody
    @RequestMapping("/delete/{lessonId}")
    public MvcReturn delete(@PathVariable Integer lessonId){
        lessonService.delete(lessonId);
        return new MvcReturn(true);
    }

    @ResponseBody
    @RequestMapping("/update")
    public MvcReturn update(Lesson l, Integer courseId){
        if (courseId==null){
            return new MvcReturn(false, "请选择课程");
        }
        l.setCourse(courseService.load(courseId));
        lessonService.update(l);
        return new MvcReturn(true);
    }

    @ResponseBody
    @RequestMapping("/toggleStatus/{lessonId}")
    public MvcReturn toggleStatus(@PathVariable Integer lessonId){
        if (lessonId==null){
            return new MvcReturn(false, "请选择课程");
        }
        Lesson l = lessonService.load(lessonId);
        if (Status.ENABLED.equals(l.getStatus())){
            l.setStatus(Status.DISABLED);
        }else {
            l.setStatus(Status.ENABLED);
        }
        lessonService.update(l);
        return new MvcReturn(true);
    }

    @RequestMapping("/generateBarcode/{lessonId}")
    public void generateBarcode(HttpServletResponse resp, @PathVariable Integer lessonId){
        ServletOutputStream fout = null;
        String url = "http://madfrog.51vip.biz/sms/admin/studentLesson/singIn/" + lessonId;
        String filename = "barcode-" + lessonId + ".jpg";
        try {
            resp.setHeader("Content-Type","application/octet-stream");
            resp.setHeader("Content-Disposition","attachment;filename="+filename);
            fout = resp.getOutputStream();
            BarcodeUtil.encode(url, fout);
            fout.flush();
            fout.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
